package com.explore.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.explore.demo.common.ResultCode;
import com.explore.demo.common.ServerResponse;
import com.explore.demo.pojo.CompanyCategory;
import com.explore.demo.mapper.CompanyCategoryMapper;
import com.explore.demo.service.CompanyCategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author gaolainan
 * @since 2021-05-04
 */
@Service
public class CompanyCategoryServiceImpl extends ServiceImpl<CompanyCategoryMapper, CompanyCategory> implements CompanyCategoryService
{

    @Resource
    private CompanyCategoryMapper companyCategoryMapper;

    @Override
    public ServerResponse addCompanyCategory(@NotNull CompanyCategory companyCategory)
    {
        return companyCategoryMapper.insert(companyCategory) > 0 ?
               ServerResponse.createBySuccessMessage("插入成功",200,null):
               ServerResponse.createBySuccessMessage("插入失败",2000,null);
    }

    @Override
    public ServerResponse deleteById(@NotNull Integer id)
    {
        QueryWrapper<CompanyCategory> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        CompanyCategory companyCategory = companyCategoryMapper.selectOne(wrapper);
        UpdateWrapper<CompanyCategory> updateWrapper = new UpdateWrapper<>();
        updateWrapper
                .set("deleted",1);
        return companyCategoryMapper.update(companyCategory,updateWrapper) != 0?
               ServerResponse.createBySuccessMessage("删除成功",
                                                     200,
                                                     null):
               ServerResponse.createBySuccessMessage("删除失败",
                                                     2000,
                                                     null);
    }

    @Override
    public ServerResponse queryAll()
    {
        QueryWrapper<CompanyCategory> wrapper = new QueryWrapper<>();
        wrapper
                .ge("id",1);
        return ServerResponse.createBySuccessMessage(ResultCode.SUCCESS.getDescription(),
                                                     ResultCode.SUCCESS.getCode(),
                                                     companyCategoryMapper.selectList(wrapper));
    }

    @Override
    public ServerResponse queryAllByCompanyId(@NotNull Integer companyId)
    {
        QueryWrapper<CompanyCategory> wrapper = new QueryWrapper<>();
        wrapper
                .eq("company_id",companyId);
        return ServerResponse.createBySuccessMessage(ResultCode.SUCCESS.getDescription(),
                                                     ResultCode.SUCCESS.getCode(),
                                                     companyCategoryMapper.selectList(wrapper));
    }

    @Override
    public ServerResponse updateCompanyCategory(@NotNull CompanyCategory companyCategory)
    {
        return companyCategoryMapper.updateById(companyCategory) != 0?
               ServerResponse.createBySuccessMessage(ResultCode.SUCCESS.getDescription(),
                                                     ResultCode.SUCCESS.getCode(),
                                                     null):
               ServerResponse.createBySuccessMessage(ResultCode.UPDATE_FAIL.getDescription(),
                                                     ResultCode.UPDATE_FAIL.getCode(),
                                                     null);
    }
}
